package com.lxw.framework.exception;


import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.ResponseResult;
import com.lxw.framework.model.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用来捕获异常类配置
 */

@ControllerAdvice
public class ExceptionCatch {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);
    //捕获 CustomException异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult customException(CustomException e) {
        ResultCode resultCode = e.getResultCode();
        e.printStackTrace();
        return new ResponseResult(resultCode);
    }


    /**
     * 捕获未知异常
     * @param e
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult unKnownException(Exception e){
        e.printStackTrace();
        return new ResponseResult(CommonCode.SERVER_ERROR);
    }
}
